﻿/*
 * File: SavedGameActionWindow.cs
 * Developer : Brent Larsen (brentoboy@gmail.com)
 * Date: 2011-AUG-9
 * Copyright (c) 2011 Brent Larsen.
 * Distributed under the terms of the GPL v3
 *  
 * This file is part of GenX-RPG
 * http://code.google.com/p/genx-rpg/ 
 * 
 * GenX-RPG is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GenX-RPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GenX-RPG.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Rpg.Game
{
    public partial class SavedGameActionWindow : Form
    {
        private int _gameId;

        public SavedGameActionWindow()
        {
            InitializeComponent();
        }

        public SavedGameActionWindow(int gameId)
            : this()
        {
            _gameId = gameId;
            this.txtName.Text = Program.Db.GetSavedGameName(gameId);
            this.btnSave.Enabled = false;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show(this, "Confirm Delete", "Are you sure?", MessageBoxButtons.OKCancel))
            {
                Program.Db.DeleteSavedGame(_gameId);

                var menuItem = Program.MainWindow.menuSavedGame.DropDownItems.OfType<ToolStripMenuItem>().Single(x => x.Tag.AsInt() == _gameId);
                Program.MainWindow.menuSavedGame.DropDownItems.Remove(menuItem);

                this.Close();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Program.Db.UpdateSavedGame(_gameId, txtName.Text);
            this.btnSave.Enabled = false;

            var menuItem = Program.MainWindow.menuSavedGame.DropDownItems.OfType<ToolStripMenuItem>().Single(x => x.Tag.AsInt() == _gameId);
            menuItem.Text = "Load Game: " + txtName.Text;
        }

        private void btnResume_Click(object sender, EventArgs e)
        {
            Program.MainWindow.OpenSavedGame(_gameId);
            this.Close();
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            this.btnSave.Enabled = true;
        }
    }
}
